﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AteneoLibri.DomainObject;
using AteneoLibriBusinessLogic.Entities;

namespace AteneoLibri.BackEnd
{
    public partial class InsertUniversity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    List<AteneoLibriBusinessLogic.Entities.University> universities = service.getUniversities();

                    List<UniversityInfo> uniList = new List<UniversityInfo>();
                    foreach (var item in universities)
                    {
                        UniversityInfo uInfo = new UniversityInfo();

                        uInfo.IdUniversity = item.IdUniversity;
                        uInfo.Name = item.Name;
                        uInfo.City = service.getCity(item.CityId.Value).Description;
                        uInfo.Nation = service.getNation(item.NationId.Value).Description;

                        uniList.Add(uInfo);
                    }

                    gvUniversities.DataSource = uniList.OrderBy(x => x.Nation).ThenBy(x => x.City).ThenBy(x => x.Name);
                    gvUniversities.DataBind();

                    ddlNations.DataSource = service.getNations().ToList();
                    ddlNations.DataBind();
                }
            }
        }

        protected void ddlNations_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                var nationId = ddlNations.SelectedItem.Value;
                if (nationId != "0")
                {
                    ddlCities.DataSource = service.getCitiesByNationId(new Guid(nationId));
                    ddlCities.DataBind();
                }
            }                       
        }

        protected void btnInsertUni_Click(object sender, EventArgs e)
        {
            string uniName = txtUniversity.Text.Trim();
            if (uniName != null && uniName != "")
            {
                try
                {
                    University uni = new University();
                    using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                    {
                        uni.IdUniversity = Guid.NewGuid();
                        uni.Name = uniName;
                        uni.NationId = new Guid(ddlNations.SelectedItem.Value);
                        uni.CityId = new Guid(ddlCities.SelectedItem.Value);

                        bool result = service.insertUniversity(uni);
                        if (result)
                        {
                            // open panel

                            // refresh page
                            Response.Redirect("InsertUniversity.aspx");
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        protected void btnDeleteUni_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton button = sender as ImageButton;
            string idUniversity = button.CommandArgument;

            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                Guid uniId = new Guid(idUniversity);
                University uni = service.getUniversity(uniId);

                List<Faculty> faculties = service.getFacultiesByUniversity(uniId);

                if (faculties.Count() == 0)
                {
                    var result = service.deleteUniversity(uni);
                    if (result)
                    {
                        // refresh page
                        Response.Redirect("InsertUniversity.aspx");
                    }
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Attenzione!", "alert('Cancellare prima tutte le facoltà dell'università prima di procedere alla rimozione dell'università.');", true);
                }



            }

        }
    }
}